package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.ChildAssessDao;
import com.example.child_protection.dao.ChildAssessValuesDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.ChildAssessDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.ChildAssessInsertRequestDTO;
import com.example.child_protection.dto.request.update.ChildAssessUpdateRequestDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.ChildAssessModel;
import com.example.child_protection.model.ChildAssessValuesModel;
import com.example.child_protection.service.ChildAssessManageService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 儿童高危信息管理service实现层
 * @date 2018-11-2
 * @author Biz
 */
@Component
public class ChildAssessManageServiceImpl extends MyBaseService implements ChildAssessManageService {

    @Autowired
    public ChildAssessDao childAssessDao;

    @Autowired
    public ChildAssessValuesDao childAssessValuesDao;

    /**
     * 添加
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insert(ChildAssessInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CHILD_ASSESS_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<ChildAssessInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(ChildAssessInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                ChildAssessModel model = new ChildAssessModel();
                BeanUtils.copyProperties(requestDTO,model);
                childAssessDao.insert(model);

                //添加
                List<ChildAssessValuesModel> models = JSONObject.parseArray(requestDTO.getAssessValues(), ChildAssessValuesModel.class);
                ChildAssessValuesModel childAssessValuesModel = new ChildAssessValuesModel();
                for(ChildAssessValuesModel m : models ){
                    childAssessValuesModel = new ChildAssessValuesModel();
                    BeanUtils.copyProperties(requestDTO,childAssessValuesModel);
                    childAssessValuesModel.setHgAssessId(m.getHgAssessId());
                    childAssessValuesModel.setAssessId(model.getId());
                    childAssessValuesDao.insert(childAssessValuesModel);
                }
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 修改
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO update(ChildAssessUpdateRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CHILD_ASSESS_UPDATE,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<ChildAssessUpdateRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(ChildAssessUpdateRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                ChildAssessModel model = new ChildAssessModel();
                BeanUtils.copyProperties(requestDTO,model);
                childAssessDao.updateSelective(model);

                //删除
                ChildAssessValuesModel delete = new ChildAssessValuesModel();
                delete.setAssessId(model.getId());
                delete.setLastUpdatePersonId(requestDTO.getLastUpdatePersonId());
                childAssessValuesDao.deleteForAssessId(delete);

                //添加
                List<ChildAssessValuesModel> models = JSONObject.parseArray(requestDTO.getAssessValues(), ChildAssessValuesModel.class);
                ChildAssessValuesModel childAssessValuesModel = new ChildAssessValuesModel();
                for(ChildAssessValuesModel m : models ){
                    childAssessValuesModel = new ChildAssessValuesModel();
                    BeanUtils.copyProperties(requestDTO,childAssessValuesModel);
                    childAssessValuesModel.setHgAssessId(m.getHgAssessId());
                    childAssessValuesModel.setAssessId(model.getId());
                    childAssessValuesDao.insert(childAssessValuesModel);
                }
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }


    /**
     * 删除
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO delete(SimpleIdRequestDTO requestDTO){
        return this.service (ServiceActionEnums.CHILD_ASSESS_DELETE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                ChildAssessModel model = new ChildAssessModel();
                BeanUtils.copyProperties(requestDTO,model);
                childAssessDao.deleteForLogic(model);
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }

}
